<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<form class="layui-form">
    名字:
    <div class="layui-inline">
        <input type="text" name="name" value="" class="layui-input" lay-affix="clear">
    </div>
	邮箱:
	<div class="layui-inline">
		<input type="text" name="email" value="" class="layui-input" lay-affix="clear">
	</div>
    部门:
    <div class="layui-inline">
        <select name="deptId" id="deptId">
            <option value="">--请选择--</option>
            <!--<option value="4">软件部</option>
            <option value="5">硬件部</option>
            <option value="6">测试部</option>-->
        </select>
    </div>
    <div class="layui-inline">
        <button class="layui-btn" lay-submit lay-filter="submitSearch">搜索</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

<table class="layui-hide" id="tableId" lay-filter="tableId"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="deleteAll">批量删除</button>
    </div>
</script>
<script type="text/html" id="imageTemplet">
    <img src="/pic/{{d.image}}">
</script>

<script th:src="@{/static/jquery-2.1.4.js}" type="text/javascript" charset="utf-8"></script>
<script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
<script th:src="@{/static/mylayer.js}" type="text/javascript" charset="utf-8"></script>

<script>
    /*layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
    })*/
    layui.use(['table','form', 'laydate', 'upload'], function(){
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload;

        $.post(
            '/dept/selectAll',
            function (result) { //回调函数
                console.log(result);
                // [{id: 4, name: '软件部', address: '信息楼1楼'},{id: 5, name: '硬件部', address: '测试楼'}]
                $(result.data).each(function () {
                    // $('#deptId').append('<option value="1">软件部</option>');
                    $('#deptId').append('<option value="'+this.id+'">'+this.name+'</option>');
                })
                form.render('select');
            },
            'json'
        )

        $.post(
            '/dept?method=selectAll',
            function (result) {
                console.log(result);
                //<option value="1">--请选择--</option>
                $(result.data).each(function () {
                    //{id: 1, name: 'Java', deleted: null, createTime: '2023-02-07 09:30:02', updateTime: 1675834791000}
                    $('#typeId').append('<option value="'+this.id+'">'+this.name+'</option>');
                });
                form.render('select');
            },
            'json'
        );


        //http://localhost:8080/admin/list?page=1&limit=10&name=zhang&email=&deptId=
        // 创建渲染实例
        table.render({
            elem: '#tableId',
            url:'/admin/list', // 此处为静态模拟数据，实际使用时需换成真实接口
            toolbar: '#toolbarDemo',
            page: true,
            cols: [
                [
                {type: 'checkbox', fixed: 'left'},
                {field:'id', fixed: 'left', title: 'ID', sort: true},
                {field:'name', title: '名字'},
                {field:'password', title: '密码'},
                {field:'deptId', title: '部门id'},
                {field:'deptName', title: '部门名'},
                {field:'email', title: '邮箱'},
                {field:'phone', title: '电话'},
                {field:'image', title: '头像', templet: '#imageTemplet'},
                {fixed: 'right', fixed: 'right',title:'操作',toolbar: '#barDemo'}
            ]
            ]
        });

        // 搜索提交
        form.on('submit(submitSearch)', function(data){
            var field = data.field; // 获得表单字段
            // 执行搜索重载
            table.reload('tableId', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: field // 搜索的字段
                // http://localhost:8080/admin?method=selectByPage&page=1&limit=10&name=zhangsan&credit=12
            });

            return false; // 阻止默认 form 跳转
        });

        // 工具栏事件
        table.on('toolbar(tableId)', function(obj){
            var id = obj.config.id;
            var checkStatus = table.checkStatus(id);
            var othis = lay(this);
            switch(obj.event){
                case 'deleteAll':
                    var data = checkStatus.data;
                    //[{id: 1, name: 'java', credit: 5},{id: 2, name: 'UI', credit: 4}]
                    console.log(data);
                    var ids = new Array();
                    $(data).each(function () {
                        ids.push(this.id);
                    })
                    //[2,4]
                    layer.confirm('真的删除行么', function(index){
                        $.post(
                            '/admin/deleteAll',
                            {'ids': ids},
                            function (result) {
                                //{code: 0, msg: '删除成功', data: null}
                                console.log(result);
                                if (result.code == 0) {
                                    mylayer.okMsg(result.msg);
                                    //删除成功之后刷新表格，展示最新数据
                                    table.reload('tableId');
                                } else {
                                    mylayer.errorMsg(result.msg);
                                }
                            },
                            'json'
                        );
                    });
                    break;
                case 'add':
                    layer.open({
                        type: 2,
                        title: '添加数据',
                        area: ['90%', '90%'],
                        content: '/admin/toAdminAdd'
                    })
                    break;
            };
        });

        //触发单元格工具事件
        table.on('tool(tableId)', function(obj){ // 双击 toolDouble
            var data = obj.data;
            //console.log(obj)
            //{id: 1, name: 'java', credit: 5}
            console.log(data);
            if(obj.event === 'del'){
				/*layer.confirm('您真的要删除么?', function () {
					//
				});*/
                layer.confirm('真的删除行么', function(index){
                    $.post(
                        '/admin/deleteById',
                        {'id': data.id},
                        function (result) {
                            //{code: 0, msg: '删除成功', data: null}
                            console.log(result);
                            if (result.code == 0) {
                                mylayer.okMsg(result.msg);
                                //删除成功之后刷新表格，展示最新数据
                                table.reload('tableId');
                            } else {
                                mylayer.errorMsg(result.msg);
                            }
                        },
                        'json'
                    );
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2,
                    title: '修改数据',
                    area: ['90%', '90%'],
                    content: '/admin/toAdminUpdate?id=' + data.id
                })
            }
        });

    });
</script>
</body>
</html>

